﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhoneBook_PC2
{
    public partial class PrincipalFrame : Form
    {
        Book phoneBook;

        public PrincipalFrame()
        {
            InitializeComponent();
            this.CenterToScreen();
            this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
            phoneBook = new Book();

            toolTipGroupList.SetToolTip(GroupListBox, "Choose a group to see details");


            this.setUpDataGridView();
            List<PhoneBook.Contact> contacts = phoneBook.getContacts();
            List<String[]> rowsList = new List<String[]>();
            foreach (PhoneBook.Contact c in contacts)
            {
                String firstName = c.getPerson().getFirstname();
                String lastName = c.getPerson().getLastname();
                String cellPhone = c.getCellnum();
                String homePhone = c.getHomenum();
                String officePhone = c.getOfficenum();
                String[] row = { firstName, lastName, cellPhone, homePhone, officePhone };
                rowsList.Add(row);
            }
            this.populateDataGridView(rowsList);

        }

        private void PrincipalFrame_Load(object sender, EventArgs e)
        {
            List<String> groupNames = phoneBook.getListGroups();
            foreach (String groupName in groupNames)
            {
                addGroupToList(groupName);
            }
        }


        private void addContactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewContact newContactFrame = new NewContact(phoneBook, this);
            newContactFrame.Show();
        }

        private void deleteContactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String firstName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            String lastName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

            PhoneBook.Contact contact = phoneBook.getContact(firstName, lastName);
            phoneBook.deleteContact(contact);

            List<PhoneBook.Contact> contacts = phoneBook.getContacts();
            List<String[]> rowsList = new List<String[]>();
            foreach (PhoneBook.Contact c in contacts)
            {
                String firstName2 = c.getPerson().getFirstname();
                String lastName2 = c.getPerson().getLastname();
                String cellPhone = c.getCellnum();
                String homePhone = c.getHomenum();
                String officePhone = c.getOfficenum();
                String[] row = { firstName2, lastName2, cellPhone, homePhone, officePhone };
                rowsList.Add(row);
            }
            this.populateDataGridView(rowsList);
        }

        private void manageGroupsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Groups groupsFrame = new Groups(this, phoneBook);
            groupsFrame.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void GroupListBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if(GroupListBox.SelectedItem != null) {
                String group = GroupListBox.SelectedItem.ToString();
                List<PhoneBook.Contact> groupContacts = phoneBook.getListGroupContacts(group);

                List<String[]> rowsList = new List<String[]>();
                foreach (PhoneBook.Contact c in groupContacts)
                {
                    String firstName = c.getPerson().getFirstname();
                    String lastName = c.getPerson().getLastname();
                    String cellPhone = c.getCellnum();
                    String homePhone = c.getHomenum();
                    String officePhone = c.getOfficenum();
                    String[] row = { firstName, lastName, cellPhone, homePhone, officePhone };
                    rowsList.Add(row);
                }
                this.populateDataGridView(rowsList);
            }
        }

        private void findButton_Click_1(object sender, EventArgs e)
        {
            String stringContact = tbFind.Text;
            List<PhoneBook.Contact> contacts = phoneBook.searchContact(stringContact);

            List<String[]> rowsList = new List<String[]>();
            foreach (PhoneBook.Contact c in contacts)
            {
                String firstName = c.getPerson().getFirstname();
                String lastName = c.getPerson().getLastname();
                String cellPhone = c.getCellnum();
                String homePhone = c.getHomenum();
                String officePhone = c.getOfficenum();
                string[] row = { firstName, lastName, cellPhone, homePhone, officePhone };
                rowsList.Add(row);
            }
            this.populateDataGridView(rowsList);
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
                dataGridView1.CurrentRow.ContextMenuStrip = this.contextMenuStrip1;

                String firstName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                String lastName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

                ViewEditContact vecFrame = new ViewEditContact(firstName, lastName, phoneBook, this);
                vecFrame.Show();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String firstName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            String lastName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

            ViewEditContact vecFrame = new ViewEditContact(firstName, lastName, phoneBook, this);
            vecFrame.Show();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String firstName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            String lastName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

            PhoneBook.Contact contact = phoneBook.getContact(firstName, lastName);
            phoneBook.deleteContact(contact);

            List<PhoneBook.Contact> contacts = phoneBook.getContacts();
            List<String[]> rowsList = new List<String[]>();
            foreach (PhoneBook.Contact c in contacts)
            {
                String firstName2 = c.getPerson().getFirstname();
                String lastName2 = c.getPerson().getLastname();
                String cellPhone = c.getCellnum();
                String homePhone = c.getHomenum();
                String officePhone = c.getOfficenum();
                String[] row = { firstName2, lastName2, cellPhone, homePhone, officePhone };
                rowsList.Add(row);
            }
            this.populateDataGridView(rowsList);
        }

        public void addGroupToList(String groupName)
        {
            GroupListBox.Items.Add(groupName);
        }

        private void setUpDataGridView()
        {
            DataGridViewCellStyle style = dataGridView1.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(dataGridView1.Font, FontStyle.Bold);

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;
            dataGridView1.BackgroundColor = Color.Honeydew;
            dataGridView1.Dock = DockStyle.Fill;

            dataGridView1.ColumnCount = 5;
            dataGridView1.Columns[0].Name = "First Name";
            dataGridView1.Columns[1].Name = "Last Name";
            dataGridView1.Columns[2].Name = "CellPhone";
            dataGridView1.Columns[3].Name = "HomePhone";
            dataGridView1.Columns[4].Name = "OfficePhone";
        }


        public void populateDataGridView(List<String[]> rowsList)
        {
            DataGridViewRowCollection rows = this.dataGridView1.Rows;
            rows.Clear();
            foreach (String[] row in rowsList)
            {
                rows.Add(row);
            }
        }

        private void callToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Info infoFrame = new Info("Calling...");
            infoFrame.Show();
        }

        private void tbFind_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                String stringContact = tbFind.Text;
                List<PhoneBook.Contact> contacts = phoneBook.searchContact(stringContact);

                List<String[]> rowsList = new List<String[]>();
                foreach (PhoneBook.Contact c in contacts)
                {
                    String firstName = c.getPerson().getFirstname();
                    String lastName = c.getPerson().getLastname();
                    String cellPhone = c.getCellnum();
                    String homePhone = c.getHomenum();
                    String officePhone = c.getOfficenum();
                    string[] row = { firstName, lastName, cellPhone, homePhone, officePhone };
                    rowsList.Add(row);
                }
                this.populateDataGridView(rowsList);
            }
        }


    }
}
